package com.sky.config;

import com.sky.interceprot.AdminLoginTokenInterceptor;
import com.sky.interceprot.UserLoginTokenInterceptor;
import com.sky.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private AdminLoginTokenInterceptor adminLoginTokenInterceptor;

    @Resource
    private UserLoginTokenInterceptor userLoginTokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截管理端的令牌校验拦截器
        registry.addInterceptor(adminLoginTokenInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/employee/login");
        // 拦截小程序端的令牌校验拦截器
        registry.addInterceptor(userLoginTokenInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/user/login", "/user/shop/status");
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展spring框架的消息转换器，在转换json格式的数据时，使用自定义的转换器.");

        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new JacksonObjectMapper());

        //把自己定义的转换器放置在第一个
        converters.add(0,messageConverter);
    }
}
